﻿using CMS.Utility;
using DBMode;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace CMS.ViewModels
{
    public class AddRecordViewModel : BindableBase, IDialogAware
    {
      
        private readonly IMethods<Commodity> commodityMethods;
        private readonly IMethods<Record> recordMethods;

        public AddRecordViewModel(IMethods<Commodity> commodityMethods, IMethods<Record> recordMethods,IEventAggregator eventAggregator)
        {
            this.commodityMethods = commodityMethods;
            this.recordMethods = recordMethods;
            Commoditys = commodityMethods.Query();
            eventAggregator.GetEvent<EventA>().Subscribe(GetString);
        }
        /// <summary>
        /// 获取事件聚合器的值
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void GetString(string obj)
        {
            int intput = recordMethods.Query().FindAll(i => i.ComName == obj && i.Type == true).Sum(i => i.Number);
            int output = recordMethods.Query().FindAll(i => i.ComName == obj && i.Type == false).Sum(i => i.Number);
            Surplus = intput - output;
        }

        public string Title { get; set; }

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
           
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            
        }

        private Record record=new Record();
        

        public Record Record
        {
            get { return record; }
            set { record = value; RaisePropertyChanged(); }
        }

        private List<Commodity> commoditys;

        public List<Commodity> Commoditys
        {
            get { return commoditys; }
            set { commoditys = value; RaisePropertyChanged(); }
        }
        public DelegateCommand<object> ConfirmCommand { get { return new DelegateCommand<object>(Confirm); } }
        public DelegateCommand<object> CancelCommand { get { return new DelegateCommand<object>(Cancel); } }

        private void Cancel(object obj)
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.No));
        }

        private int surplus;

        public int Surplus
        {
            get { return surplus; }
            set { surplus = value;RaisePropertyChanged(); }
        }


        private void Confirm(object obj)
        {
            if (Record.ComName == null || Record.Type == null || Record.Number == null) return;
            Record.MemberName = AppInstance.GetInstance.Member.Name;
            int intput = recordMethods.Query().FindAll(i => i.ComName == Record.ComName && i.Type == true).Sum(i => i.Number);
            int output = recordMethods.Query().FindAll(i => i.ComName == Record.ComName && i.Type == false).Sum(i => i.Number);
            if (intput < output) return;
            int a = recordMethods.Add(Record);
            if (a == 0) MessageBox.Show("添加失败");
            else
            {
                MessageBox.Show("添加成功");
                //IDialogParameters dialogParameters = new DialogParameters();
                //dialogParameters.Add("","")
                RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
            }
        }
    }
}
